﻿using System;
using System.IO.IsolatedStorage;

namespace Coding4Fun.PanoHead
{
    public class AppSettings
    {
        // Our isolated storage settings
        readonly IsolatedStorageSettings _settings;

        // The isolated storage key names of our settings
        const string TurnTimeKeyName = "TurnTime";
        const string SettleTimeKeyName = "SettleTime";
        const string PauseTimeKeyName = "PauseTime";
        const string IterationsKeyName = "Iterations";
        const string LastDeviceKeyName = "lastdevice";

        // The default value of our settings
        const uint TurnTimeDefault = 150;
        const uint SettleTimeDefault = 2000;
        const uint PauseTimeDefault = 2000;
        const uint IterationsDefault = 6;
        const string LastDeviceDefault = "";

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings()
        {
            // Get the settings for this application.
            _settings = IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (_settings.Contains(Key))
            {
                // If the value has changed
                if (_settings[Key] != value)
                {
                    // Store the new value
                    _settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                _settings.Add(Key, value);
                valueChanged = true;
            }
           return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (_settings.Contains(key))
            {
                value = (T)_settings[key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            _settings.Save();
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public string LastDevice
        {
            get
            {
                return GetValueOrDefault(LastDeviceKeyName, LastDeviceDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastDeviceKeyName, value))
                {
                    Save();
                }
            }
        }


        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public uint TurnTime
        {
            get
            {
                return GetValueOrDefault(TurnTimeKeyName, TurnTimeDefault);
            }
            set
            {
                if (AddOrUpdateValue(TurnTimeKeyName, value))
                {
                   Save();
                }
            }
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public uint SettleTime
        {
            get
            {
                return GetValueOrDefault(SettleTimeKeyName, SettleTimeDefault);
            }
            set
            {
                if (AddOrUpdateValue(SettleTimeKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public uint PauseTime
        {
            get
            {
                return GetValueOrDefault(PauseTimeKeyName, PauseTimeDefault);
            }
            set
            {
                if (AddOrUpdateValue(PauseTimeKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public uint Iterations
        {
            get
            {
                return GetValueOrDefault(IterationsKeyName, IterationsDefault);
            }
            set
            {
                if (AddOrUpdateValue(IterationsKeyName, value))
                {
                    Save();
                }
            }
        }


    }
}